import TimeTool from "../Tool/TimeTool";
import PlayerPrefs from "./PlayerPrefs";
import GameAdsManager from "../Ad/GameAdsManager";
import MathTool from "../Tool/MathTool";


export enum SoundType {
    MenueBGAudio = "MenueBGAudio",//主界面背景音乐
    BattleBGM = "BattleBGM", //战斗背景音乐
    ButtonClick = "ButtonClick",//按钮点击
    PlayerRevive = "PlayerRevive",//主角复活
    GetBlood = "GetBlood",//主角回血
    Combine = "Combine",//合成
    HeroDie = "HeroDie",//主角死亡
    Retreat = "Retreat",//后撤
    EnemySwordAndKnife = "EnemySwordAndKnife",//主角被刀剑斧匕击（利刃）
    EnemyHitByBlun = "EnemyHitByBlun",//主角被锤、棒、手击（钝器）
    JinzhongShow = "JinzhongShow",//金钟罩出现
    JinzhongzhaoHurt1 = "JinzhongzhaoHurt1",//金钟罩被攻击1
    JinzhongzhaoHurt2 = "JinzhongzhaoHurt2",//金钟罩被攻击2
    HeroSward1 = "HeroSward1",//剑攻击1
    HeroSward2 = "HeroSward2", //剑攻击2
    HeroSward3 = "HeroSward3",  //剑攻击3
    HeroSward4 = "HeroSward4",//剑攻击4
    HeroSward5 = "HeroSward5",// 剑攻击5
    HeroQuan1 = "HeroQuan1",//拳攻击1
    HeroQuan2 = "HeroQuan2",//拳攻击2
    HeroQuan3 = "HeroQuan3", //拳攻击3
    HeroQuan4 = "HeroQuan4",//拳攻击4
    HeroQuan5 = "HeroQuan5",  //拳攻击5
    HeroKnife1 = "HeroKnife1",  //刀攻击1
    HeroKnife2 = "HeroKnife2", //刀攻击2
    HeroKnife3 = "HeroKnife3", //刀攻击3
    HeroKnife4 = "HeroKnife4", //刀攻击4
    HeroKnife5 = "HeroKnife5",  //刀攻击5
    EnemyDead = "EnemyDead",//怪物死亡被击中
    EnemyHurt1 = "EnemyHurt1",//怪物被击1
    EnemyHurt2 = "EnemyHurt2",//怪物被击2
    EnemyHurt3 = "EnemyHurt3",//怪物被击3
    EnemyHurt4 = "EnemyHurt4", //怪物被击4
    EnemyHurt5 = "EnemyHurt5",//怪物被击5
    EnemyHurt6 = "EnemyHurt6", //怪物被击6
    HammerBearBossEnemyAttack = "HammerBearBossEnemyAttack", //大熊锤攻击
    AxeBearBossEnemyAttack = "AxeBearBossEnemyAttack",//大熊斧攻
    SwordSmallEnemyAttack = "SwordSmallEnemyAttack",   //巨剑精英怪攻击（落下+普攻）
    ChangeWeapon = "ChangeWeapon",//更换装备
    WeaponLevelUp = "WeaponLevelUp", //装备升级
    StartUp = "StartUp", //装备升星、皮肤解锁、皮肤升星
    KongFaUp = "KongFaUp",    //功法升级 
    Chuanci = "Chuanci",//穿刺
    Leitingwanjun = "Leitingwanjun",//雷霆万钧/落雷
    Lindongzhijian = "Lindongzhijian",//凛冬之剑
    Linglongbaota = "Linglongbaota",//玲珑宝塔
    Liuxingluo = "Liuxingluo",//流星落/落石
    Sanweizhenhuo = "Sanweizhenhuo",//三味真火
    Yuanshenpikan = "Yuanshenpikan",//元神劈砍
    Zhuoxinjian = "Zhuoxinjian",//灼心剑
    Baofengzhinu = "Baofengzhinu",//暴风之怒
    PlayerDie = "PlayerDie",//玩家死亡
    LevelStart = "LevelStart",//开始关卡
    PickUp = "PickUp",//拾取道具
    PickUpMoney="PickUpMoney",//拾取金币
    SelectMiji = "SelectMiji",//选择秘籍
    BearDie = "BearDie",//大熊怪死亡
    Victory="Victory",//胜利
    Defeat="Defeat",//失败
}

export class AudioClip {
    url: SoundType;
    preTime = 0;
    minRate = 0;
    public constructor(p_url: SoundType, pminRate) {
        this.url = p_url;
        this.minRate = pminRate;
    }

    
    public Play() {
        if (TimeTool.Time - this.preTime < this.minRate)
            return;
        SoundMgr.instance.PlayOneShot(this.url);
        this.preTime = TimeTool.Time;
    }

}

export default class SoundMgr {


    private  m_audioFolderPath = "ParticleScene/Audios/";

    private static minstance: SoundMgr=null;

    public static get instance() {
        if (SoundMgr.minstance == null) SoundMgr.minstance = new SoundMgr();
        return SoundMgr.minstance;
    }

    /**音效是否能播放*/
    static IsPlaySound(): boolean {
        return PlayerPrefs.GetInt('PlaySound', 1) == 1;
    }

    /**设置音效是否能播放 */
    static SetPlaySound(isPlaySound: boolean) {
        PlayerPrefs.SetInt('PlaySound', isPlaySound ? 1 : 0);
    }

    /**背景音效是否能播放*/
    static IsPlayMusic(): boolean {
        return PlayerPrefs.GetInt('PlayMusic', 1) == 1;
    }

    /**设置背景音效是否能播放*/
    static SetPlayMusic(isPlayMusic: boolean) {
        PlayerPrefs.SetInt('PlayMusic', isPlayMusic ? 1 : 0);
    }


    curbgm_SoundChannel: Laya.SoundChannel;
    constructor() {
    }

    init() {
        GameAdsManager.Instance.OnShow(this.ReSumeBgm.bind(this));
        //广告视频关闭后重新播放声音 只有vivo需要
        // GameSample.glEvent.on(EventType.AD_VIDEO_CLOSE_EVENT, this, this.onAD_VIDEO_CLOSE_EVENT);
    }

    onAD_VIDEO_CLOSE_EVENT() {
        if (Laya.Browser.onVVMiniGame)
            this.ReSumeBgm();
    }


    PlayOneShot(soundtype: SoundType) {
        let url=this.GetMusicByMusicType(soundtype);
        if (SoundMgr.IsPlaySound()) {
            Laya.SoundManager.playSound(url, 1);
        }
    }

    /**后台从前台恢复声音 */
    public ReSumeBgm() {
        console.log('ReSumeBgm');
        let isPlaySound = SoundMgr.IsPlayMusic();
        if (!isPlaySound) return;
        if (this.curbgm_SoundChannel != null)
            this.curbgm_SoundChannel.resume();
    }

    public pauseBgm() {
        if (this.curbgm_SoundChannel != null)
            this.curbgm_SoundChannel.pause();
    }

    prebgmurl = "";
    PlayBgm(musictype: SoundType, loop: boolean=true) {
        let url=this.GetMusicByMusicType(musictype);
        if (this.prebgmurl != url) this.curbgm_SoundChannel = null;
        let isPlaySound = SoundMgr.IsPlayMusic();
        console.log('isPlaySound=' + isPlaySound);
        if (isPlaySound) {
            console.log('PlayBgm');
            let num = 1;
            if (loop) num = 0;
            //使用缓存变量，防止ios上第二次播放音乐不了的问题
            if (this.curbgm_SoundChannel == null) {
                let channel = Laya.SoundManager.playMusic(url, num);
                this.curbgm_SoundChannel = channel;
            }
            else{
                this.curbgm_SoundChannel.play();
            }   
        }
    }

    private  GetMusicByMusicType(musicType: string) {
        let musicname = "";
        console.error("GetMusicByMusicType is null");
        // let musicConfig = KongFuLoader.Instance.GetMusicConfigByType(musicType);
        // if (musicConfig != null)
        //     musicname = this.m_audioFolderPath + musicConfig.MusicName; 
        // else 
        //     console.error("GetMusicByMusicType not found music " + musicType);
        return musicname;
    }

    /**播放金钟罩 */
    public  PlayJingzhongZhaoHurt()  {
        let index = MathTool.GetRandomInt(0, 2);
        if (index < 1)
            SoundMgr.instance.PlayOneShot(SoundType.JinzhongzhaoHurt1);
        else SoundMgr.instance.PlayOneShot(SoundType.JinzhongzhaoHurt2);
    }

    /**播放受伤音效 */
    public  PlayEnemyHurt() {
        let index = MathTool.GetRandomInt(1, 7);
        switch (index) {
            case 1:
                this.PlayOneShot(SoundType.EnemyHurt1);
                break;
            case 2:
                this.PlayOneShot(SoundType.EnemyHurt2);
                break;
            case 3:
                this.PlayOneShot(SoundType.EnemyHurt3);
                break;
            case 4:
                this.PlayOneShot(SoundType.EnemyHurt4);
                break;
            case 5:
                this.PlayOneShot(SoundType.EnemyHurt5);
                break;
            case 6:
                this.PlayOneShot(SoundType.EnemyHurt6);
                break;
            default:
                console.error("PlayEnemyHurt Audios Is Not Enough");
                break;
        }
    }


    
    public  MuteMusic() {
       SoundMgr.SetPlayMusic(false);
       this.pauseBgm();
    }

    public  OpenMusic() {
        SoundMgr.SetPlayMusic(true);
        this.ReSumeBgm();
    }

    //背景音乐
    public  StopBackMusic() {
        this.pauseBgm();
    }


    public  MuteSound() {
       SoundMgr.SetPlaySound(false);
    }

    public  OpenAllSound() {
        SoundMgr.SetPlaySound(true);
    }

}